import 'package:flutter/material.dart';

void main() {
  runApp(MeituanApp());
}

class MeituanApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '美团',
      theme: ThemeData(
        primarySwatch: Colors.yellow,
        primaryColor: Color(0xFFFFC107),
        scaffoldBackgroundColor: Color(0xFFF5F5F5),
      ),
      home: MeituanHomePage(),
    );
  }
}

class MeituanHomePage extends StatefulWidget {
  @override
  _MeituanHomePageState createState() => _MeituanHomePageState();
}

class _MeituanHomePageState extends State<MeituanHomePage> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: [
          HomeTab(),
          OrderTab(),
          DiscoverTab(),
          ProfileTab(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        selectedItemColor: Color(0xFFFFC107),
        unselectedItemColor: Colors.grey,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.receipt_long),
            label: '订单',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.explore),
            label: '发现',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
    );
  }
}

class HomeTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          expandedHeight: 120,
          floating: false,
          pinned: true,
          backgroundColor: Color(0xFFFFC107),
          flexibleSpace: FlexibleSpaceBar(
            title: Text('美团', style: TextStyle(color: Colors.white)),
            background: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [Color(0xFFFFC107), Color(0xFFFF9800)],
                ),
              ),
            ),
          ),
          actions: [
            IconButton(
              icon: Icon(Icons.search, color: Colors.white),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(Icons.message, color: Colors.white),
              onPressed: () {},
            ),
          ],
        ),
        SliverToBoxAdapter(
          child: Padding(
            padding: EdgeInsets.all(16),
            child: Column(
              children: [
                // 搜索栏
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(25),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.grey.withOpacity(0.2),
                        spreadRadius: 1,
                        blurRadius: 3,
                      ),
                    ],
                  ),
                  child: Row(
                    children: [
                      Icon(Icons.location_on, color: Color(0xFFFFC107)),
                      SizedBox(width: 8),
                      Text('当前位置', style: TextStyle(color: Colors.grey)),
                      Spacer(),
                      Icon(Icons.search, color: Colors.grey),
                    ],
                  ),
                ),
                SizedBox(height: 20),

                // 服务分类
                GridView.count(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  crossAxisCount: 5,
                  mainAxisSpacing: 16,
                  crossAxisSpacing: 16,
                  children: [
                    ServiceItem(
                        icon: Icons.restaurant, label: '美食', color: Colors.red),
                    ServiceItem(
                        icon: Icons.shopping_cart,
                        label: '商超',
                        color: Colors.blue),
                    ServiceItem(
                        icon: Icons.local_hospital,
                        label: '医药',
                        color: Colors.green),
                    ServiceItem(
                        icon: Icons.hotel, label: '酒店', color: Colors.purple),
                    ServiceItem(
                        icon: Icons.movie, label: '电影', color: Colors.orange),
                    ServiceItem(
                        icon: Icons.car_rental,
                        label: '打车',
                        color: Colors.teal),
                    ServiceItem(
                        icon: Icons.cleaning_services,
                        label: '家政',
                        color: Colors.indigo),
                    ServiceItem(
                        icon: Icons.face, label: '美妆', color: Colors.pink),
                    ServiceItem(
                        icon: Icons.sports_esports,
                        label: '游戏',
                        color: Colors.cyan),
                    ServiceItem(
                        icon: Icons.more_horiz,
                        label: '更多',
                        color: Colors.grey),
                  ],
                ),
                SizedBox(height: 20),

                // 轮播图
                Container(
                  height: 150,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12),
                    gradient: LinearGradient(
                      colors: [Color(0xFFFFC107), Color(0xFFFF9800)],
                    ),
                  ),
                  child: Center(
                    child: Text(
                      '限时优惠活动',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 20),

                // 推荐商家
                Text(
                  '推荐商家',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 16),

                // 商家列表
                ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: 5,
                  itemBuilder: (context, index) {
                    return RestaurantCard(
                      name: '美味餐厅 ${index + 1}',
                      rating: 4.5 - (index * 0.1),
                      deliveryTime: '30-45分钟',
                      deliveryFee: '¥5',
                      minOrder: '¥20',
                      imageUrl: 'https://via.placeholder.com/80x80',
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

class ServiceItem extends StatelessWidget {
  final IconData icon;
  final String label;
  final Color color;

  ServiceItem({required this.icon, required this.label, required this.color});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          width: 50,
          height: 50,
          decoration: BoxDecoration(
            color: color.withOpacity(0.1),
            borderRadius: BorderRadius.circular(25),
          ),
          child: Icon(icon, color: color, size: 24),
        ),
        SizedBox(height: 8),
        Text(
          label,
          style: TextStyle(fontSize: 12),
          textAlign: TextAlign.center,
        ),
      ],
    );
  }
}

class RestaurantCard extends StatelessWidget {
  final String name;
  final double rating;
  final String deliveryTime;
  final String deliveryFee;
  final String minOrder;
  final String imageUrl;

  RestaurantCard({
    required this.name,
    required this.rating,
    required this.deliveryTime,
    required this.deliveryFee,
    required this.minOrder,
    required this.imageUrl,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 16),
      padding: EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 3,
          ),
        ],
      ),
      child: Row(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Image.network(
              imageUrl,
              width: 80,
              height: 80,
              fit: BoxFit.cover,
              errorBuilder: (context, error, stackTrace) {
                return Container(
                  width: 80,
                  height: 80,
                  color: Colors.grey[300],
                  child: Icon(Icons.restaurant, color: Colors.grey),
                );
              },
            ),
          ),
          SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  name,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 4),
                Row(
                  children: [
                    Icon(Icons.star, color: Color(0xFFFFC107), size: 16),
                    SizedBox(width: 4),
                    Text('$rating'),
                    SizedBox(width: 16),
                    Text('$deliveryTime'),
                  ],
                ),
                SizedBox(height: 4),
                Text(
                  '配送费$deliveryFee | 起送$minOrder',
                  style: TextStyle(color: Colors.grey),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class OrderTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我的订单'),
        backgroundColor: Color(0xFFFFC107),
      ),
      body: ListView(
        padding: EdgeInsets.all(16),
        children: [
          // 订单状态
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              OrderStatusItem(icon: Icons.payment, label: '待付款', count: 2),
              OrderStatusItem(
                  icon: Icons.local_shipping, label: '待收货', count: 1),
              OrderStatusItem(icon: Icons.rate_review, label: '待评价', count: 3),
              OrderStatusItem(
                  icon: Icons.assignment_return, label: '退款/售后', count: 0),
            ],
          ),
          SizedBox(height: 20),

          // 订单列表
          Text(
            '全部订单',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 16),

          // 示例订单
          OrderCard(
            restaurantName: '美味餐厅',
            orderTime: '2024-01-15 12:30',
            totalAmount: '¥68.00',
            status: '已完成',
            items: ['宫保鸡丁', '麻婆豆腐', '米饭'],
          ),
          OrderCard(
            restaurantName: '川菜馆',
            orderTime: '2024-01-14 18:45',
            totalAmount: '¥45.50',
            status: '配送中',
            items: ['水煮鱼', '青椒肉丝'],
          ),
        ],
      ),
    );
  }
}

class OrderStatusItem extends StatelessWidget {
  final IconData icon;
  final String label;
  final int count;

  OrderStatusItem(
      {required this.icon, required this.label, required this.count});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Stack(
          children: [
            Container(
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                color: Color(0xFFFFC107).withOpacity(0.1),
                borderRadius: BorderRadius.circular(25),
              ),
              child: Icon(icon, color: Color(0xFFFFC107)),
            ),
            if (count > 0)
              Positioned(
                right: 0,
                top: 0,
                child: Container(
                  padding: EdgeInsets.all(4),
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Text(
                    count.toString(),
                    style: TextStyle(color: Colors.white, fontSize: 10),
                  ),
                ),
              ),
          ],
        ),
        SizedBox(height: 8),
        Text(label, style: TextStyle(fontSize: 12)),
      ],
    );
  }
}

class OrderCard extends StatelessWidget {
  final String restaurantName;
  final String orderTime;
  final String totalAmount;
  final String status;
  final List<String> items;

  OrderCard({
    required this.restaurantName,
    required this.orderTime,
    required this.totalAmount,
    required this.status,
    required this.items,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 16),
      padding: EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 3,
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                restaurantName,
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
              Text(
                status,
                style: TextStyle(
                  color: status == '已完成' ? Colors.green : Color(0xFFFFC107),
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
          SizedBox(height: 8),
          Text(
            orderTime,
            style: TextStyle(color: Colors.grey, fontSize: 12),
          ),
          SizedBox(height: 8),
          Text(
            items.join('、'),
            style: TextStyle(color: Colors.grey[600]),
          ),
          SizedBox(height: 8),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                totalAmount,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFFFFC107),
                ),
              ),
              Row(
                children: [
                  TextButton(
                    onPressed: () {},
                    child: Text('再来一单'),
                  ),
                  SizedBox(width: 8),
                  ElevatedButton(
                    onPressed: () {},
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Color(0xFFFFC107),
                    ),
                    child: Text('评价'),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class DiscoverTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('发现'),
        backgroundColor: Color(0xFFFFC107),
      ),
      body: ListView(
        padding: EdgeInsets.all(16),
        children: [
          // 热门活动
          Text(
            '热门活动',
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 16),

          // 活动卡片
          Container(
            height: 120,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12),
              gradient: LinearGradient(
                colors: [Colors.pink, Colors.purple],
              ),
            ),
            child: Center(
              child: Text(
                '限时秒杀活动',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          SizedBox(height: 20),

          // 推荐内容
          Text(
            '推荐内容',
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 16),

          // 内容卡片
          DiscoverCard(
            title: '美食探店',
            subtitle: '发现城市里的美味',
            imageUrl: 'https://via.placeholder.com/300x200',
          ),
          DiscoverCard(
            title: '生活服务',
            subtitle: '便捷生活从这里开始',
            imageUrl: 'https://via.placeholder.com/300x200',
          ),
          DiscoverCard(
            title: '娱乐休闲',
            subtitle: '享受美好时光',
            imageUrl: 'https://via.placeholder.com/300x200',
          ),
        ],
      ),
    );
  }
}

class DiscoverCard extends StatelessWidget {
  final String title;
  final String subtitle;
  final String imageUrl;

  DiscoverCard({
    required this.title,
    required this.subtitle,
    required this.imageUrl,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 3,
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.vertical(top: Radius.circular(12)),
            child: Image.network(
              imageUrl,
              height: 150,
              width: double.infinity,
              fit: BoxFit.cover,
              errorBuilder: (context, error, stackTrace) {
                return Container(
                  height: 150,
                  color: Colors.grey[300],
                  child: Icon(Icons.image, size: 50, color: Colors.grey),
                );
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 4),
                Text(
                  subtitle,
                  style: TextStyle(color: Colors.grey),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class ProfileTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我的'),
        backgroundColor: Color(0xFFFFC107),
        actions: [
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: () {},
          ),
        ],
      ),
      body: ListView(
        children: [
          // 用户信息
          Container(
            padding: EdgeInsets.all(20),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Color(0xFFFFC107), Color(0xFFFF9800)],
              ),
            ),
            child: Row(
              children: [
                CircleAvatar(
                  radius: 30,
                  backgroundColor: Colors.white,
                  child: Icon(Icons.person, size: 30, color: Color(0xFFFFC107)),
                ),
                SizedBox(width: 16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '美团用户',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 4),
                      Text(
                        '会员等级：黄金会员',
                        style: TextStyle(color: Colors.white70),
                      ),
                    ],
                  ),
                ),
                Icon(Icons.arrow_forward_ios, color: Colors.white),
              ],
            ),
          ),

          // 功能菜单
          Container(
            margin: EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.1),
                  spreadRadius: 1,
                  blurRadius: 3,
                ),
              ],
            ),
            child: Column(
              children: [
                ProfileMenuItem(
                  icon: Icons.favorite,
                  title: '我的收藏',
                  subtitle: '收藏的商家和商品',
                ),
                Divider(height: 1),
                ProfileMenuItem(
                  icon: Icons.location_on,
                  title: '收货地址',
                  subtitle: '管理收货地址',
                ),
                Divider(height: 1),
                ProfileMenuItem(
                  icon: Icons.payment,
                  title: '支付管理',
                  subtitle: '银行卡、支付宝等',
                ),
                Divider(height: 1),
                ProfileMenuItem(
                  icon: Icons.support_agent,
                  title: '客服中心',
                  subtitle: '在线客服、帮助中心',
                ),
              ],
            ),
          ),

          // 设置菜单
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.1),
                  spreadRadius: 1,
                  blurRadius: 3,
                ),
              ],
            ),
            child: Column(
              children: [
                ProfileMenuItem(
                  icon: Icons.notifications,
                  title: '消息通知',
                  subtitle: '推送设置',
                ),
                Divider(height: 1),
                ProfileMenuItem(
                  icon: Icons.security,
                  title: '隐私设置',
                  subtitle: '账户安全',
                ),
                Divider(height: 1),
                ProfileMenuItem(
                  icon: Icons.help,
                  title: '帮助与反馈',
                  subtitle: '常见问题',
                ),
                Divider(height: 1),
                ProfileMenuItem(
                  icon: Icons.info,
                  title: '关于美团',
                  subtitle: '版本信息',
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class ProfileMenuItem extends StatelessWidget {
  final IconData icon;
  final String title;
  final String subtitle;

  ProfileMenuItem({
    required this.icon,
    required this.title,
    required this.subtitle,
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: Color(0xFFFFC107).withOpacity(0.1),
          borderRadius: BorderRadius.circular(20),
        ),
        child: Icon(icon, color: Color(0xFFFFC107)),
      ),
      title: Text(title),
      subtitle: Text(subtitle, style: TextStyle(fontSize: 12)),
      trailing: Icon(Icons.arrow_forward_ios, size: 16),
      onTap: () {},
    );
  }
}
